<?php

 
/*
 * Initialization
 */

//
// 1. Start timer
//

$timeStart = microtime();

//
// 2. Debug mode on/off
//

define("DEBUG", False);

//
// 3. Libraries
//

require_once 'lib/logic.php';
#require_once 'lib/lib.mysql.php';      // MySQL lib
require_once 'lib/lib.pdo.php';         // PDO lib
#require_once 'lib/lib.csv.php';        // CSV lib
require_once 'lib/tmpl.html.php';      // HTML templates
require_once 'lib/presentation.php';   // Presentation class

//
// 4. Getting data array
// (abstracting from the request method)
// Query structure: index.php?mode=[add|delete|update]&<params>
// <params>: name, text, msgid
//

if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
    $data = $_POST;
}
else 
{
	$data = $_GET;
}
traceVar($data, "Input data from forms:");



/*
 * Input
 */

//
// 1. Initializing the guestbook
//

#$gb = new MySQLGuestbook('localhost', 'gb', 'pp".S{b\\', 'gb');
$gb = new PDOGuestbook('localhost', 'gb', 'pp".S{b\\', 'gb');
#$gb = new CSVGuestbook();
$gb->SetMessagesPerPage(2);

//
// 2. Handling the input data
//

$postedMessage = array();
$postedComment = array();
list($postedMessage, $postedComment) = HandleData($data, $gb);



/*
 * Output
 */

//
// 1. Pages
//

// Total pages in guestbook
$pages = $gb->CountPages();

// Selected page
if ( isset($data['page']) )
{
    $page = $data['page'];
    
    // If selected page > total pages, selecting last
    if ( $page >= $pages )
    {
        $page = $pages - 1;
    }
}
else 
{
    $page = 0;
}

//
// 2. Initializing the templater
//

$globalVars = array('title' => get_class($gb), 
                    'page' => $page, 
                    'pages' => $pages);
                    
$pr = new HTMLPresentation($tmpl, $globalVars); 

//
// 3. Displaying all
//

Display($pr, $gb, $postedMessage, $postedComment, $page, $timeStart);

?>